import { useState } from "react"

function useTelDown(time=60,cb) {
    const [count,setCount]=useState(time)
    const [isCurt,setIsCurt]=useState(false)

    const start=()=>{
        setIsCurt(true)
        const timer=setInterval(()=>{
          setCount(prev=>{
            if(prev<=1){
                clearInterval(timer)
                cb && cb()
                setIsCurt(false)
            }
            return prev-1
          })
        },1000)
    }
    const reset=()=>{
        setIsCurt(false)
        setCount(time)
    }
  return {isCurt,count,start,reset}
}

export default useTelDown
